<template>
  <div class="bgcBox">
    <div class="main">
      <div class="topBox">
        <i class="el-icon-arrow-left" @click="$router.go(-1)"></i>
        <h3>发布帖子</h3>
      </div>
      <div class="centerBox">
        <div class="textpart">
          <p>标题</p>
          <el-input
            placeholder="请输入标题"
            v-model="title"
            maxlength="50"
            show-word-limit
          ></el-input>
          <br>
          <br>
          <p>发布文字</p>
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 20}"
            placeholder="请输入内容"
            v-model="textarea"
          ></el-input>
          <br>
          <br>
        </div>
        <div class="cannelChoose">
          <span class="channelName">频道：</span>
          <span
            class="item"
            v-for="(item,index) in channelList"
            :key="index"
            :class="{selected : selectedChannel === item}"
            @click="selectChannel(item,index)"
          >{{ item }}</span>
        </div>
        <div class="imgpart">
          <p>上传图片</p>
          <el-upload
            action="http://121.37.242.119:20000/pss/images/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </div>
        <el-button @click="submitPost()" type="primary">确认发布</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from '../config/http';

export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      textarea: '',
      channelId: 0,
      selectedChannel: '',
      channelList: ["考研考公", "技术", "生活", "书籍", "四六级"],
      uploadedFiles: [], // 新增一个数组来存储上传的文件
      title: '',
      urlList:[]
    };
  },
  methods: {
    handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
    async handleSuccess(response, file, fileList) {
      console.log('上传成功');
      console.log('文件信息:', file);
      // 调用上传图片接口
      const formData = new FormData();
      formData.append('file', file.raw);  // 使用 file.raw 获取原始文件对象
      
      const result = await axios.post('/pss/images/upload', 
        formData,
        {headers: {'Content-Type': 'multipart/form-data'}}
      );
      console.log("上传图片接口返回结果:",result.data.data);
      this.urlList.push(result.data.data);
      console.log("urlList:",this.urlList);
      

      
      console.log('文件列表:', fileList);
      // 将文件信息添加到 uploadedFiles 数组中
      this.uploadedFiles.push({
        name: file.name,
        url: file.url || URL.createObjectURL(file.raw)
      });
    },
    // 发布帖子
    async submitPost() {
      if (!this.title.trim()) {
        this.$message.error("请填写标题");
        return;
      }
      if (!this.textarea.trim()) {
        this.$message.error("请填写要发布的文字内容");
        return;
      }
      if (!this.selectedChannel) {
        this.$message.error("请选择一个频道！");
        return;
      }

      // 发布帖子的数据，包含图片地址
      const postData = {
        title: this.title,
        content: this.textarea,
        channelId: this.channelId,
        image: [...this.urlList]  // 使用展开运算符创建新数组，避免响应式对象
      };
      
      console.log("发布帖子数据:",postData);

      const result = await axios.post('/pss/post/addOrEdit', postData,
        {
          headers:{
            'Content-Type':'application/json'
          }
        }
      );
      console.log("发布帖子接口返回结果:",result.data);
      this.$message.success("发布成功！");
      this.$router.push('/post');
    },

    // 确认选择的话题
    selectChannel(channel, index) {
      this.selectedChannel = channel;
      this.channelId = index + 1; // 修改为从1开始的索引
      console.log(this.channelId, this.selectedChannel);
    },
  }
};
</script>

<style lang="less" scoped>
.bgcBox {
  min-height: 100vh;
  background: linear-gradient(135deg, #e3ecfa 0%, #f9f9f9 100%);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 40px 0;
}
.main {
  width: 480px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 32px rgba(25, 118, 210, 0.10);
  padding: 32px 32px 24px 32px;
  margin-top: 40px;
}
.topBox {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  .el-icon-arrow-left {
    font-size: 28px;
    color: #1976d2;
    cursor: pointer;
    margin-right: 12px;
    transition: color 0.2s;
    &:hover { color: #ff9800; }
  }
  h3 {
    flex: 1;
    text-align: center;
    font-size: 24px;
    font-weight: 700;
    color: #223354;
    margin: 0;
  }
}
.centerBox {
  display: flex;
  flex-direction: column;
  gap: 28px;
}
.textpart {
  p {
    font-size: 16px;
    color: #1976d2;
    margin-bottom: 8px;
    font-weight: 600;
  }
  .el-input__inner, textarea {
    border-radius: 12px;
    font-size: 16px;
    padding: 16px;
    min-height: 100px;
    border: 1.5px solid #e0e3e7;
    transition: border 0.2s;
    &:focus { border-color: #1976d2; }
  }
}
.cannelChoose {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  .channelName {
    font-size: 16px;
    color: #1976d2;
    font-weight: 600;
    margin-right: 8px;
  }
  .item {
    background: #f4f6fa;
    color: #1976d2;
    border-radius: 16px;
    padding: 6px 18px;
    font-size: 15px;
    cursor: pointer;
    border: 1.5px solid transparent;
    transition: all 0.2s;
    &:hover { background: #e3ecfa; border-color: #1976d2; }
    &.selected {
      background: #1976d2;
      color: #fff;
      border-color: #1976d2;
      font-weight: 700;
    }
  }
}
.imgpart {
  p {
    font-size: 16px;
    color: #1976d2;
    font-weight: 600;
    margin-bottom: 8px;
  }
  .el-upload {
    width: 100%;
    .el-upload-list--picture-card {
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      .el-upload-list__item {
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(25,118,210,0.08);
      }
      .el-upload-list__item-thumbnail {
        border-radius: 12px;
        object-fit: cover;
      }
    }
    .el-upload--picture-card {
      border-radius: 12px;
      border: 2px dashed #e0e3e7;
      background: #f4f6fa;
      color: #1976d2;
      font-size: 32px;
      transition: border 0.2s;
      &:hover { border-color: #1976d2; }
    }
  }
}
.el-button {
  width: 100%;
  height: 48px;
  font-size: 18px;
  border-radius: 12px;
  background: linear-gradient(90deg, #1976d2 0%, #1565c0 100%);
  color: #fff;
  font-weight: 700;
  margin-top: 18px;
  transition: background 0.2s;
  &:hover { background: #ff9800; color: #fff; }
}
@media (max-width: 600px) {
  .main { width: 98vw; padding: 12px; }
}
</style>